package com.ddyx.service.user.app.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ddyx.common.framework.datasource.DataSource;
import com.ddyx.common.utils.token.TokenUtil;
import com.ddyx.service.user.api.entity.User;
import com.ddyx.service.user.api.vo.UserVo;
import com.ddyx.service.user.app.system.mapper.UserMapper;
import com.ddyx.service.user.app.system.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户信息表 服务实现类
 * </p>
 *
 * @author agula
 * @since 2019-07-11
 */
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    UserMapper userMapper;

    @DataSource
    @Override
    public void addUser(UserVo userVo) {
        /*对象转换*/
        MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        MapperFacade mapper = mapperFactory.getMapperFacade();

        User user = mapper.map(userVo, User.class);
        save(user);
        //userMapper.insert(user);
        log.info("添加用户成功");
    }

    @Override
    public User getUserByUnionId(String unionId) {
        return null;
    }

    @DataSource("slave")
    @Override
    public User getUserByPhone(String phone) {
        log.info("根据手机查询用户信息");
        return userMapper.getUserByPhone(phone);
    }

    @Override
    public User getUserByToken(String token) {
        return null;
    }

    @Override
    public User login(String phone, String verifyCode) {

        User user = getUserByPhone(phone);
        if (user != null) {
            user.setToken(TokenUtil.generateTokenString());
            user.setLoginTime(new Date());
            user.setActiveTime(new Date());
            userMapper.updateById(user);

            user = userMapper.selectById(user.getId());
        }
        return user;
    }

    @Override
    public int updateUser(UserVo userVo) {
        return 0;
    }


    @Override
    public User getUserInfo(int userId) {
        log.info("根据id查询用户信息");
        return userMapper.selectById(userId);
    }

    @Override
    public void logout(User user) {

    }

    @DataSource
    @Override
    public List<User> getUserList() {
        log.info("查询用户列表getUserList");
        QueryWrapper<User> userWrapper = new QueryWrapper<>();
        userWrapper.eq("status", 1);
        userWrapper.orderByAsc("create_time");
        List<User> users = userMapper.selectList(userWrapper);
        return users;
    }

    @Override
    public Map<String, Object> getUserAndLiving(Integer fromUserId, Integer toUserId) {
        return null;
    }


    @Override
    public void updateActiveDate(Integer userId) {

    }

}
